package com.lagou.edu.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.edu.common.util.ConvertUtils;
import com.lagou.edu.course.api.dto.*;
import com.lagou.edu.course.entity.po.Course;
import com.lagou.edu.course.mapper.CourseMapper;
import com.lagou.edu.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author leo
 * @since 2020-06-17
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public IPage<Course> selectPage(Page page, Wrapper wrapper) {
        Page selectPage = courseMapper.selectPage(page, null);
        return selectPage;
    }


    @Override
    @Transactional
    public void courseAutoOnline() {
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 1).set("update_time", LocalDateTime.now());
        updateWrapper.eq("status", 0).isNotNull("auto_online_time")
                .le("auto_online_time", LocalDateTime.now());
        this.update(updateWrapper);
    }

    @Transactional
    public Integer add(CourseAddDTO courseAddDTO) {
        Course course = ConvertUtils.convert(courseAddDTO, Course.class);
        this.save(course);
        return course.getId();
    }

    @Transactional
    public void update(CourseUpdateDTO courseUpdateDTO) {
        Course course = ConvertUtils.convert(courseUpdateDTO, Course.class);
        this.updateById(course);
    }

    @Transactional
    public void online(Integer id) {
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 1).set("update_time", LocalDateTime.now());
        updateWrapper.eq("status", 0).eq("id", id);
        this.update(updateWrapper);
    }

    @Override
    public void unonline(Integer id) {
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 0).set("update_time", LocalDateTime.now());
        updateWrapper.eq("status", 1).eq("id", id);
        this.update(updateWrapper);
    }

    @Transactional
    public Course getCourseById(Integer id) {
        return this.getById(id);
    }

    @Transactional
    public PageResultDTO<CourseDTO> getQueryCourses(CourseQueryParamDTO dto) {
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();

        Page<Course> page = this.page(new Page<>(dto.getCurrentPage().longValue(), dto.getPageSize().longValue()),lambda);

        PageResultDTO<CourseDTO> PageResultDTn = new PageResultDTO();
        PageResultDTn.setCurrent(dto.getCurrentPage());
        PageResultDTn.setSize(dto.getPageSize());

        List<CourseDTO> collect = page.getRecords().stream().map(c -> {
            return ConvertUtils.convert(c, CourseDTO.class);
        }).collect(Collectors.toList());
        PageResultDTn.setRecords(collect);
        PageResultDTn.setTotal(page.getTotal());
        return PageResultDTn;
    }

}
